<?php

namespace App\Observers;

use App\Models\Topic;

// creating, created, updating, updated, saving,
// saved,  deleting, deleted, restoring, restored

class TopicObserver
{
    public function saving(Topic $topic)
    {
//        $topic->body = clean($topic->body, 'user_topic_body'); // XSS过滤
        $topic->excerpt = make_excerpt($topic->body);
    }

    /**
     * 话题删除后，删除回复
     *
     * @param Topic $topic
     */
    public function deleted(Topic $topic)
    {
        // 为了防止再次Eloquent时间，使用DB类进行操作
        \DB::table('replies')->where('topic_id', $topic->id)->delete();
    }
}